package br.com.fiap.vendas.common.locator;

import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 * Classe que implementa o ServiceLocatorPartern utilizado pelos
 * BusinessDelegates
 * 
 * @author jeferson
 * 
 */
public class ServiceLocator {

	private static ServiceLocator locatorInstance;
	private InitialContext context;
	
	private ServiceLocator() {
		try {
			this.context = new InitialContext(ContextEnvironment.JNDI_PROPERTIES);
		} catch (NamingException e) {
			e.printStackTrace();
		}catch (Exception e) {
			System.err.println("ERROOOOOOOOOO");
			e.printStackTrace();// TODO: handle exception
		}
	}

	/**
	 * retorna instancia unica do serviceLocator (Singleton)
	 * @return
	 */
	public static ServiceLocator getInstance() {

		synchronized (ServiceLocator.class) {
			if (locatorInstance == null) {
				locatorInstance = new ServiceLocator();
			}
			return locatorInstance;
		}
	}
	
	public Object lookup(final String serviceJndiName ){
		try {
			return context.lookup(serviceJndiName);
		} catch (NamingException e) {
			e.printStackTrace();
			return null;
		}
	}
}
